<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="1000px" @confirm="handleSubmit"
			@close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
				<el-row>
					<el-col :span="12">
						<el-form-item label="分类" prop="pid">
							<el-select class="flex-1" v-model="formData.category_id" clearable placeholder="请选择分类">
								<el-option v-for="(item, index) in categoryList.list" :key="index" :label="item.cate_name"	:value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="名称" prop="name">
							<el-input v-model="formData.name" clearable placeholder="请输入名称" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="网站" prop="url">
							<el-input v-model="formData.url" clearable placeholder="请输入网站" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="浏览量" prop="click_actual">
							<el-input v-model="formData.click_actual" clearable placeholder="请输入浏览量" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="显示" required prop="is_show">
							<el-radio-group v-model="formData.is_show">
								<el-radio :label="1">是</el-radio>
								<el-radio :label="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="排序" prop="sort">
							<el-input type="number" v-model="formData.sort" clearable placeholder="请输入" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-form-item label="图片" prop="image">
					<material-picker v-model="formData.image" />
				</el-form-item>
				<el-form-item label="内容" prop="content">
					<editor class="flex-1 h-editor" v-model="formData.content" :height="500" />
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>

<script lang="ts" setup name="guideEdit">
	import type { FormInstance } from 'element-plus'
	import Popup from '@/components/popup/index.vue'
	import { apiGuideAdd, apiGuideEdit, apiGuideDetail } from '@/api/guide'
	import { apiSonClassifyListList } from '@/api/guide_category'
	import { timeFormat } from '@/utils/util'
	import type { PropType } from 'vue'
	defineProps({
		dictData: {
			type: Object as PropType<Record<string, any[]>>,
			default: () => ({})
		}
	})
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')


	// 弹窗标题
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑导航列表' : '新增导航列表'
	})

	// 表单数据
	const formData = reactive({
		id: '',
		name: '',
		image: '',
		url: '',
		content: '',
		click_actual: '',
		is_show: 1,
		sort: '',
		category_id: '',
	})


	// 表单验证
	const formRules = reactive<any>({
		is_show: [{
			required: true,
			message: '请选择是否显示:1-是.0-否',
			trigger: ['blur']
		}],
		category_id: [{
			required: true,
			message: '请输入分类id',
			trigger: ['blur']
		}],
	})
	
	const categoryList = reactive({
		list:[{
			id:0,
			cate_name:'顶级分类'
		}]
	})
	
	const getClasslist = async () =>{
		const Classlist = await apiSonClassifyListList()
		categoryList.list = Classlist;
	}


	// 获取详情
	const setFormData = async (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}


	}

	const getDetail = async (row : Record<string, any>) => {
		const data = await apiGuideDetail({
			id: row.id
		})
		setFormData(data)
	}


	// 提交按钮
	const handleSubmit = async () => {
		await formRef.value?.validate()
		const data = { ...formData, }
		mode.value == 'edit'
			? await apiGuideEdit(data)
			: await apiGuideAdd(data)
		popupRef.value?.close()
		emit('success')
	}

	//打开弹窗
	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}

	getClasslist()

	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>

<style>
.h-editor{
	height: 350px !important;
}
</style>

